home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11875 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  49 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.new-york.net!actcom!news
  3. From: goren@shani.net (Redrum)
  4. Subject: Re: random numbers
  5. X-Nntp-Posting-Host: teleport39.shani.co.il
  6. Message-ID: <DoDG4G.2zz@actcom.co.il>
  7. Sender: news@actcom.co.il (News)
  8. Organization: ACTCOM - Internet Services in Israel
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <4ickm0$m8l@nova.umuc.edu>
  11. Date: Sat, 16 Mar 1996 17:46:52 GMT
  12.  
  13. srussell@nova.umuc.edu (Steve Russell) wrote:
  14.  
  15. >I just got a project from an instructor for what should be a
  16. >simple program --- simulating the roll of the dice.  The whole
  17. >thing is executing the stdlib.h function rand() ( rand()%6+1)
  18. >to get a random number between 1 & 6.
  19.  
  20. >The problem is I keep getting the *same* number.  I didn't
  21. >read the library description, I don't have my book with me
  22. >and it is a 45min walk home.  I assumed it was of type int
  23. >and needed no inputs.
  24.  
  25. >Does anybody know anything about this function?
  26.  
  27. >Thanks
  28. >Steve
  29.  
  30. Steve,
  31. This happens because of a pecularity of the rand() fn. The computer
  32. doesn't really generate a random number - it uses a fixed list of
  33. random numbers!
  34.  
  35. The easiest way to  make it "random" is to use the randomize() macro.
  36.  
  37. This is a macro which initializes the random number generator with a
  38. random value. Since randomize() does it by calling the time() fn,
  39. you'll need to #include <time.h> in your program, then place the line:
  40. randomize();
  41. before you start the loop.
  42.  
  43. HTH,
  44.  
  45.  
  46. Nave Goren   <goren@shani.net>
  47. -"Of course my Cabbage needs no introduction"...
  48.  
  49.